package com.my.test.java.two.moredata.datas.share;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.my.test.java.two.moredata.datas.AbsResultSet;

public class ShareResultSet extends AbsResultSet{

	protected List<ResultSet> resultSets;
	
	private boolean flag = true;
	private int index = 0;
	
	@Override
	public boolean next() throws SQLException{
		boolean result = flag ? firstNext() : getAfter();
		flag = false;
        return result;
	}
	public boolean firstNext() throws SQLException{
		return autoProcessNext();
	}
	
	public boolean getAfter() throws SQLException{
		return processCurrent() || (!isOutOfIndex() && processNext());
	}
	
	private boolean processCurrent() throws SQLException {
        return indexResultSet.next();
    }
	private boolean isOutOfIndex(){
		return index >= resultSets.size()-1;
	}
	private boolean processNext() throws SQLException{
		ResultSet resultSet = getResultSets().get(++index);
		setIndexResultSet(resultSet);
		return autoProcessNext();
	}
	private boolean autoProcessNext() throws SQLException {
//		return indexResultSet.first();
		return indexResultSet.next();
	}
//	private boolean autoProcessNext() throws SQLException {
//		if(null==indexResultSet){
//			return false;
//		}
//		if(indexResultSet.next()){
//			return true;
//		}
//		if(!indexResultSet.next()&&resultSets.size()==1){
//			return indexResultSet.next();
//		}
//		if(!indexResultSet.next()&&index>=(resultSets.size()-1)){
//			return false;
//		}
//		if(!indexResultSet.next()&&resultSets.size()>1&&index<resultSets.size()){
//			ResultSet resultSet = getResultSets().get(++index);
//			setIndexResultSet(resultSet);
//		}
//		return autoProcessNext();
//	}

	public List<ResultSet> getResultSets() {
		return resultSets;
	}

	public void setResultSets(List<ResultSet> resultSets) {
		this.resultSets = resultSets;
	}
	
}
